package com.yuan.member.service.impl;

import cn.common.enums.SmsEnum;


import com.yuan.common.redis.constant.CacheConstants;
import com.yuan.common.redis.service.RedisRepository;
import com.yuan.member.mapper.MemberMapper;
import com.yuan.member.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Random;

@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    private RedisRepository redisRepository;
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public void sendMail(String mailbox, SmsEnum type) {
          //1.获取密钥
          //2.设置短信文本内容
        String exchangeName = "尊敬的用户";
        //3.验证码随机数
        Random rand = new Random();
        int code = rand.nextInt(100000) + 100000;
        String msg = "【" + exchangeName + "】您本次操作郵箱驗證碼為:" + code + "有效期5分鐘，如非本人操作請忽略！";
         //4.添加缓存有效时间
        System.out.println(msg);
        //redisRepository.setExpire("test", msg);
    }

    @Override
    public Boolean sendSms(String phone, String phoneCode, SmsEnum type) {
        //1.获取密钥
        //2.设置短信文本内容
        String exchangeName = "尊敬的用户";
        //3.验证码随机数
        Random rand = new Random();
        int code = rand.nextInt(100000) + 100000;
        String str="【" + exchangeName + "】Dear users, your dynamic verification code is: "+code+", valid within 5 minutes, please do not disclose to others!";
        if(null==phoneCode || phoneCode.isEmpty() || phoneCode.equals("86")){
            str = "【" + exchangeName + "】親愛的用戶，您的動態驗證碼是："+code+"，5分鐘內有效，請不要洩露給他人！";
        }
        //4.添加缓存有效时间
        redisRepository.setExpire(CacheConstants.DEFAULT_CODE_KEY + phone +"-"+type,String.valueOf(code),300);
        //5.添加记录
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("account",phone);
        hashMap.put("msg",str);
        hashMap.put("type",type);
        memberMapper.insertMessage(hashMap);
        return Boolean.TRUE;
    }
}
